package cn.yyh.serviceOrder.controller;

import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.GetTokenUtil;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceOrder.domain.AdoptOrder;
import cn.yyh.serviceOrder.domain.ProductOrder;
import cn.yyh.serviceOrder.query.AdoptOrderQuery;
import cn.yyh.serviceOrder.query.ProductOrderQuery;
import cn.yyh.serviceOrder.service.IProductOrderDetailService;
import cn.yyh.serviceOrder.service.IProductOrderService;
import cn.yyh.serviceOrder.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/serviceOrder/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;
    @Autowired
    private IProductOrderDetailService detailService;

    @PostMapping("submit")
    public R submit(@RequestBody Map<String,Object> map, HttpServletRequest request) {
        try {
            String payData = productOrderService.submit(map,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(), GetTokenUtil.getToken(request));
            return R.oK().setResultObj(payData);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("getById/{id}")
    public R getByIdOrder(@PathVariable Long id) {
        try {
            ProductOrder byId = productOrderService.getById(id);
            return R.oK().data("product", byId);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("update")
    public void update(@RequestBody ProductOrder productOrder) {
        try {
            productOrderService.update(productOrder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("admin")
    public R admin(@RequestBody ProductOrderQuery query, HttpServletRequest request) {
        try {
            PageInfo<ProductOrder> list = productOrderService.queryByAdmin(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK().data("rows", list.getRows()).data("totals", list.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("user")
    public R user(@RequestBody ProductOrderQuery query, HttpServletRequest request) {
        try {
            PageInfo<ProductOrder> list = productOrderService.queryByUser(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));

            List<ProductOrder> orders = detailService.getByOrderId(list.getRows());
            return R.oK().data("rows", orders).data("totals", list.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("successful/{id}")
    public R successful(@PathVariable Long id) {
        try {
            productOrderService.successful(id);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("cancel/{id}")
    public R cancel(@PathVariable Long id,HttpServletRequest request) {
        try {
            productOrderService.cancel(id,
                    ContextUtil.getLoginInfo(request).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("confirm/{id}")
    public R confirm(@PathVariable Long id) {
        try {
            productOrderService.confirm(id);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }
}
